package com.lightkits.wms.domain.pickreport;

import com.lightkits.common.CommonToolsUtil;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.pickreport.service.PickReportDomainService;
import com.lightkits.wms.dto.pickreport.ExecutePickReportRequestType;
import com.lightkits.wms.dto.pickreport.ExecutePickReportResponseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ExecutePickReportExecutor
 *
 * @author zhuangwei.zheng@hand-china.com
 * Created on 2019/5/8 15:53
 */
@Component
public class ExecutePickReportExecutor implements ServiceExecutor<ExecutePickReportRequestType, ExecutePickReportResponseType> {

    private final PickReportDomainService pickReportDomainService;

    @Autowired
    public ExecutePickReportExecutor(PickReportDomainService pickReportDomainService) {
        this.pickReportDomainService = pickReportDomainService;
    }

    @Override
    public ExecutePickReportResponseType service(ExecutePickReportRequestType request) {
        ExecutePickReportResponseType response = new ExecutePickReportResponseType();
        response.setSuccessful(true);
        try {
            IResult checkData = pickReportDomainService.checkParam(request);
            if (!checkData.isSuccessful()) {
                return checkData.fill(response);
            }
            CheckResult<ExecuteResModel> executeResult = pickReportDomainService
                    .executePickReport(request);
            if (!executeResult.isSuccessful()) {
                return executeResult.fill(response);
            }
            pickReportDomainService.startTransaction(executeResult.getData());
        } catch (Exception e) {
            CommonToolsUtil.executeException(e, response);
        }
        return response;
    }
}
